<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport(this)" >导入全表Excel</Button>
        </form>

        <input v-model="gameId" placeholder="gameId" ></input>
        <input v-model="gameIndex" placeholder="gameIndex" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出全表EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" ></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑NewPlayerResultConfig"
        v-model="editNewPlayerResultConfigModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="100">
        <Form-item label="gameId">
            <Input v-model="formEdit.gameId" disabled ></Input>
        </Form-item>
        <Form-item label="gameIndex">
            <Input v-model="formEdit.gameIndex" disabled></Input>
        </Form-item>
        <Form-item label="resultIndexs1">
            <Input v-model="formEdit.resultIndexs1" ></Input>
        </Form-item>
        <Form-item label="resultIndexs2">
            <Input v-model="formEdit.resultIndexs2"></Input>
        </Form-item>
        <Form-item label="resultIndexs3">
            <Input v-model="formEdit.resultIndexs3"></Input>
        </Form-item>
        <Form-item label="resultIndexs4">
            <Input v-model="formEdit.resultIndexs4"></Input>
        </Form-item>
        <Form-item label="resultIndexs5">
            <Input v-model="formEdit.resultIndexs5"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getNewPlayerResultConfigs, modifyNewPlayerResultConfig, importNewPlayerResultConfigs, getAllNewPlayerResultConfigs} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      editNewPlayerResultConfigModal: false,
      results: '',
      gameId: '',
      gameIndex: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      formEdit: {
        gameId: '',
        gameIndex: '',
        resultIndexs1: '',
        resultIndexs2: '',
        resultIndexs3: '',
        resultIndexs4: '',
        resultIndexs5: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tGameId = 0
      if (this.gameId !== '') {
        tGameId = parseInt(this.gameId)
      }
      let tGameIndex = 0
      if (this.gameIndex !== '') {
        tGameIndex = parseInt(this.gameIndex)
      }

      console.log('tGameId:', tGameId, ',tGameIndex:', tGameIndex, ',crtPage:', this.crtPage, ',pageSize:', this.pageSize)
      getNewPlayerResultConfigs(this.crtPage, this.pageSize, tGameId, tGameIndex).then((data) => {
        if (!data) {
          return
        }

        this.tableData = []
        let NewPlayerResultConfigVO = getVO('NewPlayerResultConfigVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = NewPlayerResultConfigVO.decode(pv.data[i])
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditNewPlayerResultConfig (index) {
      let data = this.tableData[index]
      this.formEdit.gameId = data.gameId
      this.formEdit.gameIndex = data.gameIndex
      this.formEdit.resultIndexs1 = data.resultIndexs1
      this.formEdit.resultIndexs2 = data.resultIndexs2
      this.formEdit.resultIndexs3 = data.resultIndexs3
      this.formEdit.resultIndexs4 = data.resultIndexs4
      this.formEdit.resultIndexs5 = data.resultIndexs5

      this.editNewPlayerResultConfigModal = true
    },
    onSubmit () {
      if (this.formEdit.gameId === '' || this.formEdit.gameIndex === '' || this.formEdit.resultIndexs1 === '' || this.formEdit.resultIndexs2 === '' || this.formEdit.resultIndexs3 === '' || this.formEdit.resultIndexs4 === '' || this.formEdit.resultIndexs5 === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let NewPlayerResultConfigVO = getVO('NewPlayerResultConfigVO')
      var newPlayerResultConfigVO = new NewPlayerResultConfigVO()
      newPlayerResultConfigVO.gameId = this.formEdit.gameId
      newPlayerResultConfigVO.gameIndex = this.formEdit.gameIndex
      newPlayerResultConfigVO.resultIndexs1 = this.formEdit.resultIndexs1
      newPlayerResultConfigVO.resultIndexs2 = this.formEdit.resultIndexs2
      newPlayerResultConfigVO.resultIndexs3 = this.formEdit.resultIndexs3
      newPlayerResultConfigVO.resultIndexs4 = this.formEdit.resultIndexs4
      newPlayerResultConfigVO.resultIndexs5 = this.formEdit.resultIndexs5

      modifyNewPlayerResultConfig(newPlayerResultConfigVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.gameId === this.formEdit.gameId && data.gameIndex === this.formEdit.gameIndex) {
              this.tableData[i].resultIndexs1 = this.formEdit.resultIndexs1
              this.tableData[i].resultIndexs2 = this.formEdit.resultIndexs2
              this.tableData[i].resultIndexs3 = this.formEdit.resultIndexs3
              this.tableData[i].resultIndexs4 = this.formEdit.resultIndexs4
              this.tableData[i].resultIndexs5 = this.formEdit.resultIndexs5
              break
            }
          }
        }
      })
    },
    onImport (obj) {
      // 导入EXCEL
      importNewPlayerResultConfigs(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      // this.$refs.tableData.exportCsv({
      //   filename: 'new_player_result_config'
      // })
      getAllNewPlayerResultConfigs().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let NewPlayerResultConfigVO = getVO('NewPlayerResultConfigVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = NewPlayerResultConfigVO.decode(pv.data[i])
          allTableData.push(vo)
        }
        console.log(allTableData)
        let filename = 'new_player_result_config'
        exportExcel(filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'gameId',
          key: 'gameId',
          align: 'center',
          width: 80
        },
        {
          title: 'gameIndex',
          key: 'gameIndex',
          align: 'center',
          width: 100
        },
        {
          title: 'resultIndexs1',
          key: 'resultIndexs1',
          align: 'center'
        },
        {
          title: 'resultIndexs2',
          key: 'resultIndexs2',
          align: 'center'
        },
        {
          title: 'resultIndexs3',
          key: 'resultIndexs3',
          align: 'center'
        },
        {
          title: 'resultIndexs4',
          key: 'resultIndexs4',
          align: 'center'
        },
        {
          title: 'resultIndexs5',
          key: 'resultIndexs5',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditNewPlayerResultConfig(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
